package com.example.myapp_1.util;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;

import androidx.annotation.NonNull;

public final class MediaUtil {

    private static final String TAG = "MainActivity";

    // 设置 来电铃声 为系统最大音量
    public static void changeVolume(@NonNull Context context) {
        int streamType = AudioManager.STREAM_RING;

        try {
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            int maxVolume = audioManager.getStreamMaxVolume(streamType);
            audioManager.setStreamVolume(streamType, maxVolume, AudioManager.FLAG_SHOW_UI);
        } catch (Exception e) {
            // Notice: 手机处于 免打扰 状态时会报异常 "java.lang.SecurityException: Not allowed to change Do Not Disturb state"
            Log.e(TAG, "" + e);
        }
    }

}
